/***********************************************************************
 * Module:  GToolbar.java
 * Author:  Voja
 * Purpose: Defines the Class GToolbar
 ***********************************************************************/

package genedit.gui;

import genedit.controller.interfaceActions.GAdjustmentHeight;
import genedit.controller.interfaceActions.GAdjustmentSize;
import genedit.controller.interfaceActions.GAdjustmentWidth;
import genedit.controller.interfaceActions.GAggregationLinkA;
import genedit.controller.interfaceActions.GAlignBottom;
import genedit.controller.interfaceActions.GAlignLeft;
import genedit.controller.interfaceActions.GAlignRight;
import genedit.controller.interfaceActions.GAlignTop;
import genedit.controller.interfaceActions.GAssociationLinkA;
import genedit.controller.interfaceActions.GClassModelA;
import genedit.controller.interfaceActions.GCompositionLinkA;
import genedit.controller.interfaceActions.GCopyA;
import genedit.controller.interfaceActions.GCutA;
import genedit.controller.interfaceActions.GDeleteA;
import genedit.controller.interfaceActions.GGeneralizationLinkA;
import genedit.controller.interfaceActions.GHandScrollA;
import genedit.controller.interfaceActions.GInterfaceModelA;
import genedit.controller.interfaceActions.GNewDocumentA;
import genedit.controller.interfaceActions.GNewPackageA;
import genedit.controller.interfaceActions.GNewProjectA;
import genedit.controller.interfaceActions.GNewSubDocumentA;
import genedit.controller.interfaceActions.GPasteA;
import genedit.controller.interfaceActions.GPolylineA;
import genedit.controller.interfaceActions.GPrintA;
import genedit.controller.interfaceActions.GRectangleA;
import genedit.controller.interfaceActions.GRedoA;
import genedit.controller.interfaceActions.GSaveA;
import genedit.controller.interfaceActions.GSaveAllA;
import genedit.controller.interfaceActions.GSearchA;
import genedit.controller.interfaceActions.GSelectA;
import genedit.controller.interfaceActions.GUndoA;
import genedit.controller.interfaceActions.GZoomBestFit;
import genedit.controller.interfaceActions.GZoomInA;
import genedit.controller.interfaceActions.GZoomOutA;

import java.awt.Dimension;

import javax.swing.AbstractAction;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.JToolBar;

public class GToolbar extends JToolBar {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected static GToolbar toolbar = null;

	protected JToggleButton select,handScroll, rectangle, polyLine, zoomIn,
							zoomOut, classModel, interfaceModel, associationLink,
							generalizationLink, aggregationLink, compositionLink;
	protected JButton bestFitZoom;
	protected ButtonGroup state;
	protected JTextField searchTextField;

	public JToggleButton getSelected() {
		if (select.isSelected())
			return select;
		if (rectangle.isSelected())
			return rectangle;
		if (polyLine.isSelected())
			return polyLine;
		if (zoomIn.isSelected())
			return zoomIn;
		if (zoomOut.isSelected())
			return zoomOut;
		if (classModel.isSelected())
			return classModel;
		if (interfaceModel.isSelected())
			return interfaceModel;
		if (handScroll.isSelected())
			return handScroll;
		if (associationLink.isSelected())
			return associationLink;
		if (generalizationLink.isSelected())
			return generalizationLink;
		if (aggregationLink.isSelected())
			return aggregationLink;
		if (compositionLink.isSelected())
			return compositionLink;
		return null;
	}

	public void setSelected(AbstractAction action) {
		select.setSelected(false);
		rectangle.setSelected(false);
		polyLine.setSelected(false);
		zoomIn.setSelected(false);
		zoomOut.setSelected(false);
		classModel.setSelected(false);
		interfaceModel.setSelected(false);
		handScroll.setSelected(false);
		associationLink.setSelected(false);
		generalizationLink.setSelected(false);
		aggregationLink.setSelected(false);
		compositionLink.setSelected(false);

		if (select.getAction().equals(action)) {
			select.setSelected(true);
		}
		if (rectangle.getAction().equals(action)) {
			rectangle.setSelected(true);
		}
		if (polyLine.getAction().equals(action)) {
			polyLine.setSelected(true);
		}
		if (zoomIn.getAction().equals(action)) {
			zoomIn.setSelected(true);
		}
		if (zoomOut.getAction().equals(action)) {
			zoomOut.setSelected(true);
		}
		if (classModel.getAction().equals(action)) {
			classModel.setSelected(true);
		}
		if (interfaceModel.getAction().equals(action)) {
			interfaceModel.setSelected(true);
		}
		if (handScroll.getAction().equals(action)) {
			handScroll.setSelected(true);
		}
		if (associationLink.getAction().equals(action)) {
			associationLink.setSelected(true);
		}
		if (generalizationLink.getAction().equals(action)) {
			generalizationLink.setSelected(true);
		}
		if (aggregationLink.getAction().equals(action)) {
			aggregationLink.setSelected(true);
		}
		if (compositionLink.getAction().equals(action)) {
			compositionLink.setSelected(true);
		}
	}

	protected GToolbar() {
		setFloatable(true);

		add(GNewDocumentA.getInstance());
		add(GNewSubDocumentA.getInstance());
		add(GNewPackageA.getInstance());
		add(GNewProjectA.getInstance());

		addSeparator();

		add(GSaveA.getInstance());
		add(GSaveAllA.getInstance());
		add(GPrintA.getInstance());

		addSeparator();
		add(GCutA.getInstance());
		add(GCopyA.getInstance());
		add(GPasteA.getInstance());
		add(GDeleteA.getInstance());

		addSeparator();

		add(GUndoA.getInstance());
		add(GRedoA.getInstance());

		addSeparator();

		state = new ButtonGroup();
		select = new JToggleButton(GSelectA.getInstance());
		select.setSelected(true);
		rectangle = new JToggleButton(GRectangleA.getInstance());
		polyLine = new JToggleButton(GPolylineA.getInstance());
		classModel = new JToggleButton(GClassModelA.getInstance());
		interfaceModel = new JToggleButton(GInterfaceModelA.getInstance());
		handScroll = new JToggleButton(GHandScrollA.getInstance());
		associationLink = new JToggleButton(GAssociationLinkA.getInstance());
		generalizationLink = new JToggleButton(GGeneralizationLinkA.getInstance());
		aggregationLink = new JToggleButton(GAggregationLinkA.getInstance());
		compositionLink = new JToggleButton(GCompositionLinkA.getInstance());

		state.add(select);
		state.add(handScroll);
		state.add(rectangle);
		state.add(polyLine);
		state.add(associationLink);
		state.add(aggregationLink);
		state.add(compositionLink);
		state.add(generalizationLink);
		state.add(classModel);
		state.add(interfaceModel);

		add(select);
		add(handScroll);
		addSeparator();
		add(rectangle);
		add(polyLine);
		add(associationLink);
		add(aggregationLink);
		add(compositionLink);
		add(generalizationLink);
		add(classModel);
		add(interfaceModel);
		addSeparator();

		zoomIn = new JToggleButton(GZoomInA.getInstance());
		zoomIn.setText("");
		state.add(zoomIn);
		add(zoomIn);

		zoomOut = new JToggleButton(GZoomOutA.getInstance());
		zoomOut.setText("");
		state.add(zoomOut);
		add(zoomOut);

		addSeparator();
		bestFitZoom = new JButton(GZoomBestFit.getInstance());
		bestFitZoom.setText("");
		add(bestFitZoom);
		// add(GZoomBestFit.getInstance());

		addSeparator();
		add(GAlignLeft.getInstance());
		add(GAlignTop.getInstance());
		add(GAlignRight.getInstance());
		add(GAlignBottom.getInstance());

		addSeparator();
		add(GAdjustmentHeight.getInstance());
		add(GAdjustmentWidth.getInstance());
		add(GAdjustmentSize.getInstance());

		addSeparator(new Dimension(60, 0));
		searchTextField = new JTextField();
		searchTextField.setMaximumSize(new Dimension(150, 28));
		add(searchTextField);
		add(GSearchA.getInstance());

		setSize(0, 30);
	}

	public static GToolbar getInstance() {
		if (toolbar == null) {
			toolbar = new GToolbar();
			toolbar.initComponents();
		}
		return toolbar;
	}

	public void initComponents() {
		zoomIn.setText("");
		zoomOut.setText("");

	}

	public JButton getBestFitZoom() {
		return bestFitZoom;
	}

	public JTextField getSearchTextField() {
		return searchTextField;
	}

	public void setSearchTextField(JTextField searchTextField) {
		this.searchTextField = searchTextField;
	}

	public JToggleButton getSelect() {
		return select;
	}

	public JToggleButton getPolyLine() {
		return polyLine;
	}

	public JToggleButton getAssociationLink() {
		return associationLink;
	}

	public JToggleButton getGeneralizationLink() {
		return generalizationLink;
	}

	public JToggleButton getAggregationLink() {
		return aggregationLink;
	}

	public JToggleButton getCompositionLink() {
		return compositionLink;
	}
	
	
	
	

}